<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  include("base.php");
  
  class SeasonController extends BaseController
  {  
    var $_name = 'season';
    
    function add(){
    
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());
      
      $view->setModel($this->getModel('division','divisionmodel'));
      parent::add();
    }
    
    function edit(){
      
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());
      
      $view->setModel($this->getModel('pic','picmodel'));
      $view->setModel($this->getModel('division','divisionmodel'));
      $view->setModel($this->getModel('seasondivision','seasondivisionmodel'));
      parent::edit();
    }
    
    function save()
    {
      $data = JRequest::get('POST');
      $data['description'] = JRequest::getVar( 'description', '', 'post', 'string', JREQUEST_ALLOWHTML );
       
      $mpic = $this->getModel('pic','picmodel');
      $pic  = $this->reconstruct_pic($data);
      $tmp  = $mpic->save($pic);
      $data['pic_id'] = $tmp->id;
     
      $model  = & $this->getModel($this->_name); 
      $season = $model->save($data);
      
      $season_division = $this->getModel('seasondivision','seasondivisionmodel');
      $season_division->renew_divisions($season->id, $data['divisions']);
       
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo,  ucfirst($this->_name) . ' Saved!');             
    }
    
    function remove()
    {
      $ids = JRequest::getVar('cid', null, 'default', 'array');
      if($ids === null)
        JError::raiseError(500, 'cid parameter missing from the request');
       
      $season_division = & $this->getModel('seasondivision', 'seasondivisionmodel');
      foreach($ids as $id){
        $season_divisions = $season_division->get_by_season_id($id);
        $sd_ids           = array();
        foreach($season_divisions as $sd)
          array_push($sd_ids, $sd->id);
        if(!empty($sd_ids))
          $season_division->delete($sd_ids);
      }
      parent::remove();
    }
    
    private function reconstruct_pic($data){
      
      $result = array();
      $result['id']          = $data['pic_id'];  
      $result['description'] = basename($data['pic_url']);
      $result['pic_type']    = 'SEASONS';
      $result['pic_id']      = $data['id'];
      $result['flickr_id']   = $data['pic_flickr_id'];
	  $result['url']         = $data['old_pic_url'];
      $result['pic_url']     = $data['pic_url'];
	  
	  if($result['id'])
        $result['updated_on'] = date('Y-m-d',time());
      else
        $result['created_on'] = date('Y-m-d',time());
      
      return $result;
      return $result;
    }
  }
?>